package GUI;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import Entita.AreaLocalizzazione;
import Entita.Tavola;
import Entita.AreaLocalizzazione.Localizzazione;

public class VisualizzaLocalizzazioni extends JFrame {
	
	private class CBox extends JLabel{
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		private int index;
		public CBox(Image image, int index){
			super(new ImageIcon(image));
			this.index=index;
		}
		public int getIndex() {
			return index;
		}
	}
	
	private void Clicked(MouseEvent e) {
		CBox source=(CBox)e.getSource();
		if (comboBox.isEnabled()){
			comboBox.setSelectedIndex(source.getIndex()+1);
		}
		this.dispose();
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JComboBox comboBox;
	
	/**
	 * Create the frame.
	 */
	public VisualizzaLocalizzazioni(Tavola tavola,JComboBox cb) {
		comboBox=cb;
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		int step=290;
		int gap=8;
		int numeroRighe=(int) (Math.ceil(tavola.getNumeroLocalizzazioni()/4.0));
		int altezza =numeroRighe*(step);
		this.setTitle("Tavola " + tavola.getNumeroLocalizzazioni()+ "("+altezza+")");
		this.setResizable(false);
		

		contentPane = new JPanel();
		this.setBounds(0, 0, 1020, 639);

		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		this.setResizable(true);
		setContentPane(contentPane);
		JPanel pannello=new JPanel( new FlowLayout( FlowLayout.LEFT, 10, 8 ) );
		pannello.setPreferredSize(new Dimension(1000,altezza+gap));
		JScrollPane bars = new JScrollPane(pannello);
		bars.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		bars.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		bars.getVerticalScrollBar().setUnitIncrement(step);
		
		contentPane.add(bars, BorderLayout.CENTER);
		int i=0;
		for(i=0;i<tavola.getNumeroLocalizzazioni();i++){
			AreaLocalizzazione local=AreaLocalizzazione.LOCALIZZAZIONE(tavola.getID()-1, i);
			CBox label=new CBox(local.getImmagine(),i);
			label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
			label.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					Clicked(e);
				}
			});
			pannello.add(label);
		}
		/*for (AreaLocalizzazione local : tavola.getLocalizzazioni()){
			i++;
			CBox label=new CBox(local.getImmagine(),i);
			label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
			label.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					Clicked(e);
				}
			});
			pannello.add(label);
		}*/
		
		
		
		
	}
}
